<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
            ${- range $index, $column := .columns}
            ${- if (ne $column.ColumnName $.pk.ColumnName)}
            <el-form-item label="${$column.Comments}" prop="${$column.AttrName}">
                <el-input v-model="dataForm.${$column.AttrName}" placeholder="${$column.Comments}"></el-input>
            </el-form-item>
            ${- end}
            ${- end}
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data() {
            return {
                visible: false,
                dataForm: {
            ${- range $index, $column := .columns}
            ${- if (eq $column.ColumnName  $.pk.ColumnName)}
            ${$column.AttrName}
        :
            0,${- else}
            ${$column.AttrName}
        :
            ''${- if(lt $index ($.columns|len))},${- end}
            ${- end}
            ${- end}
        },
            dataRule: {
                ${- range $index, $column := .columns}
                ${- if (ne $column.ColumnName  $.pk.ColumnName)}
                ${$column.AttrName}
            :
                [
                    {required: true, message: '${$column.Comments}不能为空', trigger: 'blur'}
                ] ${- if(lt $index  ($.columns|len))},${- end}
                ${- end}
                ${- end}
            }
        }
        },
        methods: {
            init(id) {
                this.dataForm.$
                {.
                    pk.AttrName
                }
                = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.${.pk.AttrName}
                )
                    {
                        this.$http({
                            url: this.$http.adornUrl(`/${.moduleName}/${.pathName}/get/${print "${this.dataForm."}${.pk.AttrName}}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                ${- range $index, $column := .columns}
                                ${- if (ne $column.ColumnName  $.pk.ColumnName)}
                                this.dataForm.$
                                {
                                    $column.AttrName
                                }
                                = data.$
                                {
                                    $.classname
                                }
                            .
                                $
                                {
                                    $column.AttrName
                                }
                                ${- end}
                                ${- end}
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/${.moduleName}/${.pathName}/${print "${!this.dataForm."}${.pk.AttrName} ? 'save' : 'update'})`),
                            method: 'post',
                            data: this.$http.adornData({
                                ${- range $index, $column := .columns}
                                ${- if (eq $column.ColumnName  $.pk.ColumnName)}
                                '${$column.AttrName}': this.dataForm.$
                        {
                            $column.AttrName
                        }
                    ||
                        undefined,
                            ${- else}
                            '${$column.AttrName}'
                    :
                        this.dataForm.$
                        {
                            $column.AttrName
                        }${- if( ne $index ($.columns|len))}
                    ,${- end}
                        ${- end}
                        ${- end}
                    })
                    }).
                        then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>
